/**
 * 
 */
package com.onewingu.gcs.server;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

/**
 * @author er66
 *
 */
public class TokenStore {

	public String getToken(String username) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			Token token = pm.getObjectById(Token.class, username);

			return token.getToken();
		} catch (JDOObjectNotFoundException e) {
			return null;
		} finally {
			pm.close();
		}
	}
	
	public Token getOAuthToken(String username) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			Token token = pm.getObjectById(Token.class, username);

			return token;
		} catch (JDOObjectNotFoundException e) {
			return null;
		} finally {
			pm.close();
		}
	}

	public void addToken(String username, String accessToken, String tokenSecret) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {
			Token token = new Token(username, accessToken, tokenSecret);
			pm.makePersistent(token);
		} finally {
			pm.close();
		}
	}
}